package com.log.blog_backend.service.impls;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.log.blog_backend.Dao.GetcommentList;
import com.log.blog_backend.common.Result;
import com.log.blog_backend.entity.ArticleComments;
import com.log.blog_backend.entity.User;
import com.log.blog_backend.mapper.ArticleCommentsMapper;
import com.log.blog_backend.mapper.UserMapper;
import com.log.blog_backend.service.ArticleCommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.log.blog_backend.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author blog
 * @since 2023-01-09
 */
@Service
@Slf4j
public class ArticleCommentsServiceImpl extends ServiceImpl<ArticleCommentsMapper, ArticleComments> implements ArticleCommentsService {

    @Autowired
    ArticleCommentsMapper articleCommentsMapper;

    @Autowired
    UserMapper userMapper;


    @Autowired
    TokenUtil tokenUtil;
    @Override
    public Result comment(ArticleComments articleComments, HttpServletRequest httpServletRequest) {
          if (StringUtils.isNotBlank(articleComments.getCommentContent())){
              String userId = tokenUtil.getUserIdByToken(httpServletRequest);
              articleComments.setCommentUserId(userId);
              int insert = articleCommentsMapper.insert(articleComments);
              if (insert>0){
                  return Result.ok(articleComments);
              }else {
                  return Result.fail("评论失败",10222);
              }
          }else {
              return Result.fail("评论不能为空",10000);
          }

    }

    @Override
    public Result getcomment(String articleId) {
         List<GetcommentList>getcommentLists=new ArrayList<>();

        QueryWrapper<ArticleComments>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("article_id",articleId);
        List<ArticleComments> articleCommentsList = articleCommentsMapper.selectList(queryWrapper);

        for (int i=0;i<articleCommentsList.size();i++){
            GetcommentList getcommentList=new GetcommentList();
            String commentUserId = articleCommentsList.get(i).getCommentUserId();

            User user = userMapper.selectById(commentUserId);
            String avatar = user.getAvatar();
            String account = user.getAccount();
            getcommentList.setUserAvatar(avatar);
            getcommentList.setAccount(account);

            BeanUtils.copyProperties(articleCommentsList.get(i),getcommentList);
            getcommentLists.add(i,getcommentList);
        }

        return Result.ok(getcommentLists);
    }

    @Override
    public Result getcommentCount(String articleId) {
        if (StringUtils.isNotBlank(articleId)){
            QueryWrapper<ArticleComments>queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("article_id",articleId);
            Integer integer = articleCommentsMapper.selectCount(queryWrapper);
            log.info(">>>>>>>>>>>>>>>"+integer);
            return Result.ok(integer);

        }else {
            return null;

        }
    }
}
